package com.miao.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.miao.base.exception.XueChengPlusException;
import com.miao.dto.XcChooseCourseDto;
import com.miao.dto.XcCourseTablesDto;
import com.miao.feignclient.ContentServiceClient;
import com.miao.mapper.XcChooseCourseMapper;
import com.miao.mapper.XcCourseTablesMapper;
import com.miao.po.XcChooseCourse;
import com.miao.po.XcCourseTables;
import com.miao.pojo.CoursePublish;
import com.miao.service.XcChooseCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class XcChooseCourseServiceImpl extends ServiceImpl<XcChooseCourseMapper, XcChooseCourse> implements XcChooseCourseService {
    @Autowired
    private ContentServiceClient contentServiceClient;
    @Autowired
    private XcChooseCourseMapper chooseCourseMapper;
    @Autowired
    private XcCourseTablesMapper courseTablesMapper;

    //选择课程
    @Transactional
    @Override
    public XcChooseCourseDto addChooseCourse(String uId, Long courseId) {


        //远程调用查看课程收费过程
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        if(coursepublish == null){
            XueChengPlusException.cast("该课程不存在！");
        }
        XcChooseCourseDto xcChooseCourseDto = new XcChooseCourseDto();
        if("201000".equals(coursepublish.getCharge())){
            //如果免费，选课表，课程表都写入
            XcChooseCourse xcChooseCourse = addFreeCourse(uId, courseId, coursepublish);
            BeanUtils.copyProperties(xcChooseCourse, xcChooseCourseDto);
            xcChooseCourseDto.setLearnStatus(getLearnStatus(uId, courseId));
            return xcChooseCourseDto;
        }
        //反之收费，选课表写入
        XcChooseCourse xcChooseCourse = addChargeCourse(uId, courseId, coursepublish);
        BeanUtils.copyProperties(xcChooseCourse, xcChooseCourseDto);
        xcChooseCourseDto.setLearnStatus(getLearnStatus(uId, courseId));
        return xcChooseCourseDto;
    }

    //添加免费课程到选课表
    private XcChooseCourse addFreeCourse(String uId, Long courseId, CoursePublish coursePublish){
        //已经选过课程就不添加了
        LambdaQueryWrapper<XcChooseCourse> xcChooseCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        xcChooseCourseLambdaQueryWrapper.eq(XcChooseCourse::getUserId, uId);
        xcChooseCourseLambdaQueryWrapper.eq(XcChooseCourse::getCourseId, courseId);
        xcChooseCourseLambdaQueryWrapper.eq(XcChooseCourse::getOrderType, "700001");//免费课程
        xcChooseCourseLambdaQueryWrapper.eq(XcChooseCourse::getStatus,"701001");//添加成功
        List<XcChooseCourse> xcChooseCourses = chooseCourseMapper.selectList(xcChooseCourseLambdaQueryWrapper);
        if(xcChooseCourses.size() != 0){
            return xcChooseCourses.get(0);
        }

        //没有添加过，选课记录表插入数据
        XcChooseCourse xcChooseCourse = new XcChooseCourse();
        xcChooseCourse.setCourseId(courseId);
        xcChooseCourse.setUserId(uId);
        xcChooseCourse.setCourseName(coursePublish.getName());
        xcChooseCourse.setCompanyId(coursePublish.getCompanyId());
        xcChooseCourse.setOrderType("700001");
        xcChooseCourse.setCreateDate(LocalDateTime.now());
        xcChooseCourse.setCoursePrice(coursePublish.getPrice());
        xcChooseCourse.setValidDays(365);//课程有效期
        xcChooseCourse.setStatus("701001");//选课成功
        xcChooseCourse.setValidtimeStart(LocalDateTime.now());//有效期开始时间
        xcChooseCourse.setValidtimeEnd(LocalDateTime.now().plusDays(365L));
        //插入
        chooseCourseMapper.insert(xcChooseCourse);

        //添加到课程表
        addToTables(xcChooseCourse);

        return xcChooseCourse;
    }

    //添加课程到课程表
    public XcCourseTables addToTables(XcChooseCourse xcChooseCourse){
        if(!"701001".equals(xcChooseCourse.getStatus())){
            XueChengPlusException.cast("无法将未添加成功的课程添加到课程表！！");
        }
        //看是否已经添加过
        LambdaQueryWrapper<XcCourseTables> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(XcCourseTables::getCourseId, xcChooseCourse.getCourseId());
        wrapper.eq(XcCourseTables::getUserId, xcChooseCourse.getUserId());
        XcCourseTables xcCourseTables = courseTablesMapper.selectOne(wrapper);
        if(xcCourseTables != null){
            //重复添加课程
            return xcCourseTables;
        }
        //添加到课程表
        xcCourseTables = new XcCourseTables();
        BeanUtils.copyProperties(xcChooseCourse, xcCourseTables);
        xcCourseTables.setChooseCourseId(xcChooseCourse.getId());//记录选课表的主键
        xcCourseTables.setCourseType(xcChooseCourse.getOrderType());//课程类型
        xcCourseTables.setUpdateDate(LocalDateTime.now());
        courseTablesMapper.insert(xcCourseTables);
        return xcCourseTables;
    }

    //添加付费课程到选课表
    private XcChooseCourse addChargeCourse(String uId, Long courseId, CoursePublish coursePublish){
        //已经选过课程就不添加了
        LambdaQueryWrapper<XcChooseCourse> xcChooseCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        xcChooseCourseLambdaQueryWrapper.eq(XcChooseCourse::getUserId, uId);
        xcChooseCourseLambdaQueryWrapper.eq(XcChooseCourse::getCourseId, courseId);
        xcChooseCourseLambdaQueryWrapper.eq(XcChooseCourse::getOrderType, "700002");//收费课程
        xcChooseCourseLambdaQueryWrapper.eq(XcChooseCourse::getStatus,"701002");//待支付
        List<XcChooseCourse> xcChooseCourses = chooseCourseMapper.selectList(xcChooseCourseLambdaQueryWrapper);
        if(xcChooseCourses.size() != 0){
            return xcChooseCourses.get(0);
        }

        //没有添加过，选课记录表插入数据
        XcChooseCourse xcChooseCourse = new XcChooseCourse();
        xcChooseCourse.setCourseId(courseId);
        xcChooseCourse.setUserId(uId);
        xcChooseCourse.setCourseName(coursePublish.getName());
        xcChooseCourse.setCompanyId(coursePublish.getCompanyId());
        xcChooseCourse.setOrderType("700002");
        xcChooseCourse.setCreateDate(LocalDateTime.now());
        xcChooseCourse.setCoursePrice(coursePublish.getPrice());
        xcChooseCourse.setValidDays(365);//课程有效期
        xcChooseCourse.setStatus("701002");//待支付
        xcChooseCourse.setValidtimeStart(LocalDateTime.now());//有效期开始时间
        xcChooseCourse.setValidtimeEnd(LocalDateTime.now().plusDays(365L));
        //插入
        chooseCourseMapper.insert(xcChooseCourse);

        return xcChooseCourse;
    }

    //获取课程学习状态
    private String getLearnStatus(String uid, Long courseId){
        //查询课程表
        LambdaQueryWrapper<XcCourseTables> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(XcCourseTables::getCourseId, courseId);
        wrapper.eq(XcCourseTables::getUserId, uid);
        XcCourseTables xcCourseTables = courseTablesMapper.selectOne(wrapper);
        if(xcCourseTables == null){
            return "702002";//没有选课或者没有支付
        }
        //是否过期
        if(xcCourseTables.getValidtimeEnd().isBefore(LocalDateTime.now())){
            return "702003";//过期了
        }
        return "702001";//正常学习
    }

    //获取学习状态，返回的是对象
    public XcCourseTablesDto getLearnStatusDto(String uid, Long courseId){
        XcCourseTablesDto xcCourseTablesDto = new XcCourseTablesDto();
        //查询课程表
        LambdaQueryWrapper<XcCourseTables> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(XcCourseTables::getCourseId, courseId);
        wrapper.eq(XcCourseTables::getUserId, uid);
        XcCourseTables xcCourseTables = courseTablesMapper.selectOne(wrapper);
        if(xcCourseTables == null){
            xcCourseTablesDto.setLearnStatus("702002");
            return xcCourseTablesDto;
            //没有选课或者没有支付
        }
        //是否过期
        if(xcCourseTables.getValidtimeEnd().isBefore(LocalDateTime.now())){
            xcCourseTablesDto.setLearnStatus("702003");
            return xcCourseTablesDto;//过期了
        }
        xcCourseTablesDto.setLearnStatus("702001");
        return xcCourseTablesDto;//正常学习
    }
    //付费之后，更新选课表以及课程表
    @Transactional
    @Override
    public boolean updateChaseCourse(String chooseId) {
        XcChooseCourse xcChooseCourse = chooseCourseMapper.selectById(chooseId);
        if(xcChooseCourse == null){
            return false;
        }
        //更新选课表状态
        String status = xcChooseCourse.getStatus();
        if(status.equals("701002")){
            xcChooseCourse.setStatus("701001");
            int i = chooseCourseMapper.updateById(xcChooseCourse);
            if(i <= 0){
                XueChengPlusException.cast("修改付费课程状态异常");
            }
        }
        //更新我的课程
        addToTables(xcChooseCourse);
        return true;
    }
}
